<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row>
          <!-- <a-col :span="24">
            <a-form-model-item label="拍摄日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="psrq">
              <a-input v-model="model.psrq" placeholder="请输入拍摄日期" ></a-input>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="拍摄日期(Enter键确认)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="psrq">
              <a-date-picker v-model="model.psrq" placeholder="请选择拍摄日期" mode="year" format="YYYY">
              </a-date-picker>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dz">
              <a-input v-model="model.dz" placeholder="请输入地址"></a-input>
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="作物类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lx">
              <j-dict-select-tag type="list" v-model="model.lx" dictCode="zwlx" placeholder="请选择作物类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="等级1" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dj1">
              <a-input v-model="model.dj1" placeholder="请输入等级1"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="等级2" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dj2">
              <a-input v-model="model.dj2" placeholder="请输入等级2"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="等级3" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dj3">
              <a-input v-model="model.dj3" placeholder="请输入等级3"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="等级4" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dj4">
              <a-input v-model="model.dj4" placeholder="请输入等级4"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="等级5" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dj5">
              <a-input v-model="model.dj5" placeholder="请输入等级5"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="等级6" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dj6">
              <a-input v-model="model.dj6" placeholder="请输入等级6"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="等级7" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dj7">
              <a-input v-model="model.dj7" placeholder="请输入等级7"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="等级8" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dj8">
              <a-input v-model="model.dj8" placeholder="请输入等级8"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import moment from 'moment'

export default {
  name: 'ZwglZwgcModal',
  components: {},
  data() {
    return {
      title: '操作',
      width: 800,
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },

      confirmLoading: false,
      validatorRules: {
        psrq: [{ required: true, message: '此处不可为空!' }],
        dz: [
              { required: true,message: '此处不可为空!'},
              { pattern: /^((ht|f)tps?):\/\/[\w\-]+(\.[\w\-]+)+([\w\-.,@?^=%&:\/~+#]*[\w\-@?^=%&\/~+#])?$/, message: '请输入正确的网址!'},
           ],
        lx: [{ required: true, message: '此处不可为空!' }],
        dj1: [{ required: true, message: '此处不可为空!' },{ pattern: /^(\d+|\d+\.\d)$/, message: '输入整数或小数点后保留1位!' }],
        dj2: [{ required: true, message: '此处不可为空!' },{ pattern: /^(\d+|\d+\.\d)$/, message: '输入整数或小数点后保留1位!' }],
        dj3: [{ required: true, message: '此处不可为空!' },{ pattern: /^(\d+|\d+\.\d)$/, message: '输入整数或小数点后保留1位!' }],
        dj4: [{ required: true, message: '此处不可为空!' },{ pattern: /^(\d+|\d+\.\d)$/, message: '输入整数或小数点后保留1位!' }],
        dj5: [{ required: true, message: '此处不可为空!' },{ pattern: /^(\d+|\d+\.\d)$/, message: '输入整数或小数点后保留1位!' }],
        dj6: [{ required: true, message: '此处不可为空!' },{ pattern: /^(\d+|\d+\.\d)$/, message: '输入整数或小数点后保留1位!' }],
        dj7: [{ required: true, message: '此处不可为空!' },{ pattern: /^(\d+|\d+\.\d)$/, message: '输入整数或小数点后保留1位!' }],
        dj8: [{ required: true, message: '此处不可为空!' },{ pattern: /^(\d+|\d+\.\d)$/, message: '输入整数或小数点后保留1位!' }],
      },
      url: {
        add: '/znnqjc/zwglZwgc/add',
        edit: '/znnqjc/zwglZwgc/edit'
      }
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      // 格式化日期
      const formattedDate = moment(this.model.psrq).format('YYYY')
      console.log('aaa', formattedDate)

      this.edit({ ...this.modelDefault, psrq: formattedDate })
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.$refs.form.clearValidate()
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    }
  },
  watch: {
    'model.psrq'(newDate) {
      // 重新选择日期后，再次格式化日期
      const formattedDate = moment(newDate).format('YYYY')
      console.log('bbb', formattedDate)

      this.model.psrq = formattedDate
    }
  }
}
</script>

